const router = require('koa-router')()
const jwt = require("jsonwebtoken");
const myKey = "214ii";
const svgCaptcha = require('svg-captcha');


// 验证码
router.get('/captcha',(ctx,next)=>{
  const captcha = svgCaptcha.create({
    size: 4,
    fontSize: 36,
    ignoreChars: '0oO1iIl', 
    noise: 3, 
    color: false,
    background: '#cc9966' ,
    height:38,
    width:44
  })
  const img = captcha.data
  const text = captcha.text
  console.log(text);
  ctx.body={
    img:img,
    text:text
  }
})

// 账号验证
router.post('/check',async(ctx,next)=>{
  let number=ctx.request.body.number;
  let pass=ctx.request.body.pass;
  let check=ctx.request.body.check;
  let code=ctx.request.body.code;
  console.log(ctx.request.body);
  let checkpass=await ctx.db.EXCUTE('select * from login where username=?',number);
  console.log('checkpass',checkpass[0]);
  // ctx.body.code  300 验证码错误
  // 100 用户名错误
  // 400 密码错误
  // 200 验证成功
  if(check != code){
    ctx.body={"code":300,"msg":"warning"};
  }else{
    if(!checkpass[0]){
      ctx.body={"code":100,"msg":"warning"};
    }
    else if(number == checkpass[0].userName &&  pass !=checkpass[0].password){
      ctx.body={"code":400,"msg":"warning"};
    }else{
      let token=jwt.sign({username:checkpass[0].userName},myKey);
      ctx.body={"code":200,"msg":"success",user:number,token:token};
    }
  }
})


let fileList=new Array() ;
router.post('/upload',async(ctx,next)=>{
  let filetype=ctx.request.body.fileType;
  console.log(ctx.request.body);
  if(filetype=="image/jpeg" ||filetype== "image/png"){
    ctx.body={"code":200,"msg":"success"};
let data=ctx.request.body;
    let fileList=await ctx.db.EXCUTE('insert into images value(?,?,?,?,?,?,?)',[Math.random(),data.fileName,data.fileSize,data.fileType,data.lastModifiedDate,data.formData,0]);
    console.log(fileList);
  }else{
    ctx.body={"code":500,"msg":"图片格式错误",fileList:fileList};
  }
})

router.get('/getlist', async (ctx, next) => {
  // ctx.body = fileList;
  let currentFilePage=ctx.request.query.currentFilePage;
  let pre=0;
  let totalList=await ctx.db.EXCUTE("select * from images");
  if(currentFilePage!=1){
    pre=(currentFilePage-1)*4;
  }
  fileList = await ctx.db.EXCUTE('select * from images limit ?,?',[pre,4]);
  // console.log(userList);
  ctx.body={fileList:fileList,total:totalList.length};
})

router.post('/del',async(ctx,next)=>{
  let id=ctx.request.body.id;
  console.log(id);
  let deleteUserSql=`delete from images where imgId='${id}'`;
  fileList = await ctx.db.EXCUTE(deleteUserSql);
  ctx.body="success";
})

router.post('/editImg',async(ctx,next)=>{
  let data=ctx.request.body;
  console.log(data);
  fileList=await ctx.db.EXCUTE('update images set imgName=?,imgSize=?,imgType=?,imgTime=? where imgId=?',[data.imgName,data.imgSize,data.imgType,data.imgTime,data.imgId])
  ctx.body={"msg":"ok","code":200}
})


router.post('/searchImg',async(ctx,next)=>{
  fileList=await ctx.db.EXCUTE('select * from images where imgName=?',ctx.request.body.imgName)
  ctx.body=fileList;
})

let imgMax; 
imgMax= fileList.length;
router.post('/upImg', async (ctx, next) => {
  imgMax += 1;
  fileList = await ctx.db.EXCUTE('select * from images');
  let data = ctx.request.body;
  console.log(data.name);

  fileList = await ctx.db.EXCUTE('update images set imgTop =? where imgName=?',[imgMax,data.name]);
  fileList = await ctx.db.EXCUTE('select * from images order by imgTop desc');
  console.log(fileList);
  ctx.body=fileList.splice(0,4);
})



let userList = [];
let currentUserPage=1;
router.get('/getUserList', async (ctx, next) => {
  currentUserPage=ctx.request.query.currentUserPage;
  let pre=0;
  let totalList=await ctx.db.EXCUTE("select * from users");
  if(currentUserPage!=1){
    pre=(currentUserPage-1)*4;
  }
  userList = await ctx.db.EXCUTE('select * from users limit ?,?',[pre,4]);
  // console.log(userList);
  ctx.body={userList:userList,total:totalList.length};
})


router.post('/editUser',async(ctx,next)=>{
  let data=ctx.request.body;
  console.log(data);
  userList=await ctx.db.EXCUTE('update users set userTel=?,userEmail=?,userAddress=? where userName=?',[data.userTel,data.userEmail,data.userAddress,data.userName])
  ctx.body={"msg":"ok","code":200}
})
router.post('/plusUser', async (ctx, next) => {
  let data=ctx.request.body;
  userList=await ctx.db.EXCUTE('insert into users value(?,?,?,?,?,?)',[Math.random(),data.userName,data.userTel,data.userEmail,data.userAddress,0]);
  ctx.body={"code":200,"msg":"success"};
})

router.post('/delUser',async(ctx,next)=>{
  let name=ctx.request.body.name;
  console.log(name);
  let deleteUserSql=`delete from users where userName='${name}'`;
  userList = await ctx.db.EXCUTE(deleteUserSql);
  ctx.body="success";
})



let userMax; 
userMax= userList.length;
router.post('/upUser', async (ctx, next) => {
  userMax += 1;
  userList = await ctx.db.EXCUTE('select * from users');
  let data = ctx.request.body;
  console.log(data.name);

  userList = await ctx.db.EXCUTE('update users set userTop =? where userName=?',[userMax,data.name]);
  userList = await ctx.db.EXCUTE('select * from users order by userTop desc');
  console.log(userList);
  ctx.body=userList.splice(0,4);
})


router.post('/searchUser',async(ctx,next)=>{
  userList=await ctx.db.EXCUTE('select * from users where userName=?',ctx.request.body.userName)
  ctx.body=userList;

})

// 线路操作
let wayList=[];
let currentSize=4;
let currentPage=1;
router.get('/getWay',async(ctx,next)=>{
  console.log(ctx.request.query.currentPage);
  currentPage=ctx.request.query.currentPage;
  let pre=0; let end=4;
  totalList=await ctx.db.EXCUTE('select * from ways');
  // console.log(currentPage);
  if(currentPage!=1){
    pre=(currentPage-1)*4;
  }
  wayList = await ctx.db.EXCUTE('select * from ways limit ?,?',[pre,4]);
  console.log(wayList);
  ctx.body={wayList:wayList,total:totalList.length};
})
router.post('/editWay', async (ctx, next) => {
  console.log(ctx.request.body);
  let data=ctx.request.body;
  wayList=await ctx.db.EXCUTE('update ways set locations=?,address=?,tel=? where wayName=?',[data.locations,data.address,data.tel,data.wayName])
  ctx.body={"msg":"ok","code":200}
})

router.post('/delWays',async (ctx, next) => {
  let name=ctx.request.body.name;
  let deleteWaySql=`delete from ways where wayName='${name}'`;
  wayList = await ctx.db.EXCUTE(deleteWaySql);
  ctx.body="success";
})

router.post('/plusWay',async(ctx,next)=>{
  console.log(ctx.request.body);
  let data=ctx.request.body;
  wayList=await ctx.db.EXCUTE('insert into ways value (?,?,?,?,?,?)',[Math.random(),data.wayName,data.address,data.locations,data.tel,0])
  ctx.body={"code":200,"msg":"success"};
})
let max;
max=wayList.length;
router.post('/upWay',async(ctx,next)=>{
  max+=1;
  wayList=await ctx.db.EXCUTE('select * from ways');
  let data=ctx.request.body;
  console.log(data);
  console.log(max);
  wayList=await ctx.db.EXCUTE('update ways set top =? where wayName=?',[max,data.wayName]);
  wayList=await ctx.db.EXCUTE('select * from ways order by top desc');
  console.log(await ctx.db.EXCUTE('select wayName from ways order by top desc'));
  ctx.body=wayList.splice(0,4);
})

router.post('/searchWay',async(ctx,next)=>{
  wayList=await ctx.db.EXCUTE('select * from ways where wayName=?',ctx.request.body.wayName)
  console.log(wayList);
  ctx.body=wayList;

})

module.exports = router
